#include <iostream>
#include <string>

const std::string space_sep = " ";
const std::string message_sep = "\n";

enum
{
    Div_Zero = 1,
    Mod_Zero , 
    No_Operator
};


// 加报头
std::string Encode(std::string& message)        // "字符串长度"\n"字符串"\n
{
    std::string package;
    package = std::to_string(message.length()) + message_sep + message + message_sep;

    return package;
}

// 解报头
std::string Decode(std::string& package)        // "字符串长度"\n"字符串"\n
{
    int pos = package.find(message_sep);
    if(pos == std::string::npos) return "";

    std::string len_str = package.substr(0 , pos);

    int message_len = std::stoi(len_str) + 2 + len_str.size();      // 该有效报文的长度
    if(message_len > package.size()) return "";


    std::string content = package.substr(pos+1 , std::stoi(len_str));

    package.erase(0 , message_len);             // 将该报文从缓冲区中裁剪出去

    return  content;
}




// 请求结构体 , 实现序列化和反序列化
class Request
{
public:
    Request()
    {}
    Request(const int data1 , const int data2 , char op)
    :data1_(data1) , data2_(data2) , op_(op)
    {}

    // 实现序列化
    std::string serialize() // 转化为 : "data1 op data2"
    {
        std::string str = std::to_string(data1_) + space_sep + op_ + space_sep + std::to_string(data2_);
        return str;
    }

    //实现反序列化   
    void deserialize(const std::string & str)           // 将字符串 "data1 op data2" 数据提取出来
    {
        int lpos = str.find(space_sep);                 // 从前往后找空格
        std::string data1_str = str.substr(0 , lpos);   
        int rpos = str.rfind(space_sep);                // 从后往前找空格
        std::string data2_str = str.substr(rpos +1);    

        op_ = str[lpos + 1];
        data1_ = std::stoi(data1_str);
        data2_ = std::stoi(data2_str);
    }

    int Get_Data1(){
        return data1_;
    }
    int Get_Data2(){
        return data2_;
    }
    char Get_Op(){
        return op_;
    }

private:
    int data1_;
    int data2_;
    char op_;
};


// 结果
class Respond
{
public:
    Respond()
    {}
    Respond(const int result , const int code)
    :result_(result) , code_(code)
    {}

    // 实现序列化
    std::string serialize() // 转化为 : "result code"
    {
        std::string str = std::to_string(result_) + space_sep + std::to_string(code_);
        return str;
    }

    //实现反序列化   
    void deserialize(const std::string & str)           // 将字符串 "result code" 数据提取出来
    {
        int pos = str.find(space_sep);
        if(pos == std::string::npos) return;
        std::string result_str = str.substr(0 , pos);
        std::string code_str = str.substr(pos + 1);

        result_ = std::stoi(result_str);
        code_ = std::stoi(code_str);
    }

    int Get_result()
    {
        return result_;
    }

    int Get_code()
    {
        return code_;
    }

private:
    int result_;        // 记录答案
    int code_;          // 记录结果是否可信
};